﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CluelessGame
{
    /// <summary>
    /// Interaction logic for GameBoard.xaml
    /// </summary>
    public partial class GameBoard : UserControl
    {
        public GameManager CurrentGameManager;

        public GameBoard()
        {
            InitializeComponent();
            RenderOptions.SetBitmapScalingMode(this, BitmapScalingMode.Fant);
        }

        private void AttemptMove(Location target)
        {
            if (!CurrentGameManager.IsLocalPlayerTurn)
            {
                return;
            }

            if (CurrentGameManager.AttemptMove(target))
            {
                if (target is Room)
                {
                    SuggestionScreen suggestionScreen = new SuggestionScreen();
                    suggestionScreen.ShowDialog();
                    if (suggestionScreen.OK)
                    {
                        CrimeCards crimeCards = new CrimeCards();
                        crimeCards.Character = suggestionScreen.SelectedCharacter.AssociatedCard;
                        crimeCards.Weapon = suggestionScreen.SelectedWeapon.AssociatedCard;
                        crimeCards.Room = (CurrentGameManager.LocalPlayer.AssociatedCharacter.CurrentLocation as Room).AssociatedCard;

                        CurrentGameManager.MakeSuggestionLocal(crimeCards);
                    }
                    else
                    {
                        CurrentGameManager.RevertMove();
                    }
                }
            }
            else
            {
                MessageBox.Show("That is not a valid move.");
            }
        }

        private void Hall_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Hall);
        }

        private void Study_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Study);
        }

        private void Lounge_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Lounge);
        }

        private void Billiardroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Billiardroom);
        }

        private void Library_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Library);
        }

        private void Diningroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Diningroom);
        }

        private void Ballroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Ballroom);
        }

        private void Conservatory_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Conservatory);
        }

        private void Kitchen_MouseUp(object sender, MouseButtonEventArgs e)
        {
            AttemptMove(GameManager.CurrentGameObjects.Kitchen);
        }

        private Hallway GetCommonHallway(Room first, Room second)
        {
            //get the hallway that the first and second room have in common.
            foreach (Hallway hall in first.AdjacentHallways)
            {
                if (second.AdjacentHallways.Contains(hall))
                {
                    return hall;
                }
            }
            return null;
        }

        private void HallwayStudHall_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Study,GameManager.CurrentGameObjects.Hall);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayHallLounge_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Hall, GameManager.CurrentGameObjects.Lounge);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayLibraryBilliardroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Library, GameManager.CurrentGameObjects.Billiardroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayBilliardroomDiningroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Billiardroom, GameManager.CurrentGameObjects.Diningroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayConservatorBallroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Conservatory, GameManager.CurrentGameObjects.Ballroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayBallroomKitchen_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Ballroom, GameManager.CurrentGameObjects.Kitchen);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayStudyLibrary_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Study, GameManager.CurrentGameObjects.Library);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayHallBilliardroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Hall, GameManager.CurrentGameObjects.Billiardroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayLoungeDiningroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Lounge, GameManager.CurrentGameObjects.Diningroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayDiningroomKitchen_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Diningroom, GameManager.CurrentGameObjects.Kitchen);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayBilliardroomBallroom_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Billiardroom, GameManager.CurrentGameObjects.Ballroom);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

        private void HallwayLibraryConservatory_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Location targetLocation = GetCommonHallway(GameManager.CurrentGameObjects.Library, GameManager.CurrentGameObjects.Conservatory);

            if (targetLocation != null)
            {
                AttemptMove(targetLocation);
            }
            else
            {
                MessageBox.Show("Unexpected error: could not identify clicked hallway.");
            }
        }

    }
}
